Advertisement
Junaid_Hossain

Stack with Linked List 2

May 17th, 2024 (edited)
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Student {
  6.     char name[50];
  7.     int reg;
  8.     double cgpa;
  9.     int semester;
  10.     int year;
  11.     int isRegular; // irregular = 0, regular = 1
  12.     struct Student* next;
  13. };
  14.  
  15. struct Student* head = NULL;
  16.  
  17. void addNewStudent(const char* Name, int reg_no, double gpa, int sem, int years, int reg) {
  18.     struct Student* ptr = (struct Student*)malloc(sizeof(struct Student));
  19.     if (ptr == NULL) {
  20.         printf("Memory allocation failed\n");
  21.         exit(1);
  22.     }
  23.     strcpy(ptr->name, Name);
  24.     ptr->reg = reg_no;
  25.     ptr->cgpa = gpa;
  26.     ptr->semester = sem;
  27.     ptr->year = years;
  28.     ptr->isRegular = reg;
  29.     ptr->next = NULL;
  30.  
  31.     if (head == NULL || head->cgpa < ptr->cgpa) {
  32.         ptr->next = head;
  33.         head = ptr;
  34.     } else {
  35.         struct Student* temp = head;
  36.         while (temp->next != NULL && ptr->cgpa < temp->next->cgpa) {
  37.             temp = temp->next;
  38.         }
  39.         ptr->next = temp->next;
  40.         temp->next = ptr;
  41.     }
  42.     printf("New Student is Added - %s\n", ptr->name);
  43. }
  44.  
  45. void delStudent(int reg_no) {
  46.     struct Student* temp = head;
  47.     struct Student* prev = NULL;
  48.  
  49.     while (temp != NULL) {
  50.         if (temp->reg == reg_no) {
  51.             if (prev == NULL) { // Deleting head node
  52.                 head = temp->next;
  53.             } else {
  54.                 prev->next = temp->next;
  55.             }
  56.             printf("Student Record is Removed - %s\n", temp->name);
  57.             free(temp);
  58.             return;
  59.         }
  60.         prev = temp;
  61.         temp = temp->next;
  62.     }
  63.     printf("Student Record with reg number %d not found\n", reg_no);
  64. }
  65.  
  66. void displayAllStudent() {
  67.     struct Student* p = head;
  68.     while (p != NULL) {
  69.         printf("%d_%s_%.2f_%d-%d_", p->reg, p->name, p->cgpa, p->year, p->semester);
  70.         if (p->isRegular)
  71.             printf("Regular\n");
  72.         else
  73.             printf("Irregular\n");
  74.         p = p->next;
  75.     }
  76. }
  77.  
  78. struct Student* awardStack = NULL;
  79.  
  80. void pushAwardee(struct Student* student) {
  81.     struct Student* newAwardee = (struct Student*)malloc(sizeof(struct Student));
  82.     if (newAwardee == NULL) {
  83.         printf("Memory allocation failed\n");
  84.         exit(1);
  85.     }
  86.     *newAwardee = *student;
  87.     newAwardee->next = awardStack;
  88.     awardStack = newAwardee;
  89.     printf("Selected Awardee Enlisted - %s\n", student->name);
  90. }
  91.  
  92. void popAwardee() {
  93.     if (awardStack == NULL) {
  94.         printf("No awardee to remove\n");
  95.         return;
  96.     }
  97.     struct Student* temp = awardStack;
  98.     awardStack = awardStack->next;
  99.     printf("Awardee Removed - %s\n", temp->name);
  100.     free(temp);
  101. }
  102.  
  103. void displayAllAwardee() {
  104.     struct Student* temp = awardStack;
  105.     while (temp != NULL) {
  106.         printf("%d_%s_%.2f_%d-%d_", temp->reg, temp->name, temp->cgpa, temp->year, temp->semester);
  107.         if (temp->isRegular)
  108.             printf("Regular\n");
  109.         else
  110.             printf("Irregular\n");
  111.         temp = temp->next;
  112.     }
  113. }
  114.  
  115. struct Student* findStudentByName(const char* name) {
  116.     struct Student* p = head;
  117.     while (p != NULL) {
  118.         if (strcmp(p->name, name) == 0) {
  119.             return p;
  120.         }
  121.         p = p->next;
  122.     }
  123.     return NULL;
  124. }
  125.  
  126. int main() {
  127.     addNewStudent("Monica", 22201999, 3.50, 1, 2, 1);
  128.     addNewStudent("Ross", 22201000, 3.92, 1, 2, 1);
  129.     addNewStudent("Chandler", 18201000, 4.0, 1, 3, 1);
  130.     addNewStudent("Janice", 21201089, 2.52, 1, 2, 0);
  131.     addNewStudent("Joey", 20201979, 2.88, 1, 2, 0);
  132.     addNewStudent("Gunther", 19201000, 4.0, 2, 3, 0);
  133.  
  134.     printf("\n");
  135.     delStudent(21201089);
  136.     printf("\n");
  137.     displayAllStudent();
  138.     printf("\n");
  139.  
  140.     // Push "Monica", "Ross", and "Chandler" to the awardee stack
  141.     struct Student* student;
  142.  
  143.     student = findStudentByName("Monica");
  144.     if (student != NULL) pushAwardee(student);
  145.  
  146.     student = findStudentByName("Ross");
  147.     if (student != NULL) pushAwardee(student);
  148.  
  149.     student = findStudentByName("Chandler");
  150.     if (student != NULL) pushAwardee(student);
  151.  
  152.     printf("\nAll Awardees:\n");
  153.     displayAllAwardee();
  154.  
  155.     return 0;
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement